package controllers

import "git.oschina.net/tinkler/for_kmjxc/models/spaceshop"

// Operations about fee 费用管理
type FeeController struct {
	HelperController
}

// @Title Get
// @Description 通过付款单编号获取费用明细
// @Param sheetNo path string true "付款单编号"
// @Success 200 {object} models.spaceshop.SupplierFeeItem
// @Failure 403 body is empty
// @router /:sheetNo [get]
func (f *FeeController) Get() {
	sheetNo := f.GetString(":sheetNo")
	if sfis, err := spaceshop.GetSupplierFeeItemsBySheetNo(sheetNo); err == nil {
		f.Data[`json`] = sfis
	}
	f.ServeAPI()
}

// @Title Create
// @Description create supplier fee item
// @Param body body models.spaceshop.SupplierFeeItem true	"The fee content"
// @Success 200 {string} models.spaceshop.SupplierFeeItem.FeeNo
// @Failure 403 body is empty
// @router / [post]
func (f *FeeController) Post() {
	if fi, err := spaceshop.CreateSupplierFeeItemFromJsonBytes(f.Ctx.Input.RequestBody); err == nil {
		if fiNo, err := spaceshop.AddFeeOnRpTRecpayRecordInfo(fi); err == nil {
			f.Data[`json`] = map[string]string{"FeeNo": fiNo}
			f.ServeAPI()
		} else {
			f.Abort("403")
		}
	} else {
		f.Abort("403")
	}

}

// @Title Update
// @Description update the supplier fee item
// @Param feeNo path string	true "the feeno"
// @Param body	body	models.spaceshop.SupplierFeeItem	true "The fee content"
// @Success 200 {string} models.spaceshop.SupplierFeeItem.FeeNo
// @Failure 403 body is empty
// @router /:feeNo [put]
func (f *FeeController) Put() {
	feeNo := f.Ctx.Input.Param(":feeNo")
	if fi, err := spaceshop.CreateSupplierFeeItemFromJsonBytes(f.Ctx.Input.RequestBody); err == nil {
		if err := spaceshop.UpdateSupplierFeeItemByFeeNo(feeNo, fi); err == nil {
			f.Data[`json`] = "update success!"
			f.ServeAPI()
		} else {
			f.Abort("403")
		}
	} else {
		f.Abort("403")
	}

}

// @Title Delete
// @Description delete the supplier fee item
// @Param feeNo	path	string	true	"The supplier fee item no"
// @Success 200 {string} delete success!
// @Failure 403 feeNo is empty
// @router /:feeNo [delete]
func (f *FeeController) Delete() {
	feeNo := f.Ctx.Input.Param(":feeNo")
	if err := spaceshop.DeleteSupplierFeeItemByFeeNo(feeNo); err == nil {
		f.Data[`json`] = "delete success!"
		f.ServeAPI()
	} else {
		f.Abort("403")
	}

}
